#ifndef CLIENTCONNECTION_H
#define CLIENTCONNECTION_H

#include <QObject>
#include <QTcpSocket>
#include <QTimer>
#include <QDateTime>
#include <QMutex>

class ClientConnection : public QObject {
    Q_OBJECT
public:
    explicit ClientConnection(qintptr socketDescriptor, QObject *parent = nullptr);
    ~ClientConnection();

    void start();
    void stop();
    void sendMessage(const QString &message);
    QString clientInfo() const;
    QDateTime connectionTime() const;
    bool isActive() const;

signals:
    void clientDisconnected(qintptr descriptor);
    void messageReceived(qintptr descriptor, const QString &message);
    void errorOccurred(qintptr descriptor, const QString &error);

private slots:
    void onReadyRead();
    void onDisconnected();
    void onError(QAbstractSocket::SocketError error);

private:
    QTcpSocket *m_socket;
    qintptr m_descriptor;
    QDateTime m_connectionTime;
    mutable QMutex m_mutex;
    bool m_active;
};

#endif // CLIENTCONNECTION_H